#ifndef PRINTERCONTROL_H
#define PRINTERCONTROL_H

#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QDebug>
#include <QMutex>

class PrinterControl : public QObject
{
    // 当前打印机类型
    Q_PROPERTY(int CurrentType READ CurrentType WRITE setCurrentType NOTIFY CurrentTypeChanged FINAL)

    // 当前语言环境
    Q_PROPERTY(bool Language_Type READ Language_Type WRITE setLanguage_Type NOTIFY Language_TypeChanged FINAL)

    // 型材高度
    Q_PROPERTY(QString MaterialHeight READ MaterialHeight WRITE setMaterialHeight NOTIFY MaterialHeightChanged FINAL)
    // 型材长度
    Q_PROPERTY(QString MaterialLength READ MaterialLength WRITE setMaterialLength NOTIFY MaterialLengthChanged FINAL)
    // 左角度
    Q_PROPERTY(QString LeftAngle READ LeftAngle WRITE setLeftAngle NOTIFY LeftAngleChanged FINAL)
    // 右角度
    Q_PROPERTY(QString RightAngle READ RightAngle WRITE setRightAngle NOTIFY RightAngleChanged FINAL)
    // 型材名称
    Q_PROPERTY(QString MaterialName READ MaterialName WRITE setMaterialName NOTIFY MaterialNameChanged FINAL)
    // 客户名称
    Q_PROPERTY(QString Customer_Name READ Customer_Name WRITE setCustomer_Name NOTIFY Customer_NameChanged FINAL)


    Q_OBJECT
public:
    // 删除拷贝构造函数和赋值运算符
    PrinterControl(const PrinterControl&) = delete;
    PrinterControl& operator=(const PrinterControl&) = delete;

    // 获取单例实例
    static PrinterControl* getInstance(QObject *parent = nullptr);

    // 设置 打印机 连接参数
    void setPrinterConnectParameter(QString serialPort,
                                    QString baudRate,
                                    QString dataBit,
                                    QString parityBit,
                                    QString stopBit);

    // 打印动作
    Q_INVOKABLE void printerActive(QString MaterialHeight,
                                   QString MaterialLength,
                                   QString LeftAngle,
                                   QString RightAngle);


    void print_Active_WeiHuang(QString MaterialHeight,
                               QString MaterialLength,
                               QString LeftAngle,
                               QString RightAngle,
                               QString MaterialName,
                               QString Customer);

    void print_Active_RuiGong(QString MaterialHeight,
                              QString MaterialLength,
                              QString LeftAngle,
                              QString RightAngle,
                              QString MaterialName,
                              QString Customer);

    void print_Active_DaPu(QString MaterialHeight,
                           QString MaterialLength,
                           QString LeftAngle,
                           QString RightAngle,
                           QString MaterialName,
                           QString Customer);

    void print_Active_WeiHuang_en_US(QString MaterialHeight,
                                     QString MaterialLength,
                                     QString LeftAngle,
                                     QString RightAngle,
                                     QString MaterialName,
                                     QString Customer);

    void print_Active_RuiGong_en_US(QString MaterialHeight,
                                    QString MaterialLength,
                                    QString LeftAngle,
                                    QString RightAngle,
                                    QString MaterialName,
                                    QString Customer);

    void print_Active_DaPu_en_US(QString MaterialHeight,
                                 QString MaterialLength,
                                 QString LeftAngle,
                                 QString RightAngle,
                                 QString MaterialName,
                                 QString Customer);

    // 连接 打印机设备
    void connectToDevice();

    QSerialPort* m_Printer;

    int CurrentType() const;
    void setCurrentType(int newCurrentType);

    QString MaterialHeight() const;
    void setMaterialHeight(const QString &newMaterialHeight);

    QString MaterialLength() const;
    void setMaterialLength(const QString &newMaterialLength);

    QString LeftAngle() const;
    void setLeftAngle(const QString &newLeftAngle);

    QString RightAngle() const;
    void setRightAngle(const QString &newRightAngle);

    QString MaterialName() const;
    void setMaterialName(const QString &newMaterialName);

    QString Customer_Name() const;
    void setCustomer_Name(const QString &newCustomer_Name);

    bool Language_Type() const;
    void setLanguage_Type(bool newLanguage_Type);

signals:
    void CurrentTypeChanged();
    void MaterialHeightChanged();
    void MaterialLengthChanged();
    void LeftAngleChanged();
    void RightAngleChanged();
    void MaterialNameChanged();
    void Customer_NameChanged();
    void Language_TypeChanged();

public slots :
    void on_AutoActiveSlots(bool left,bool right,QString materialLength,QString materialHeight);

private:
    // 私有构造函数
    PrinterControl(QObject *parent = nullptr);
    ~PrinterControl();

    // 静态实例指针
    static PrinterControl* m_instance;
    // 线程安全互斥锁
    static QMutex m_mutex;

    int m_CurrentType;
    QString m_MaterialHeight;
    QString m_MaterialLength;
    QString m_LeftAngle;
    QString m_RightAngle;
    QString m_MaterialName;
    QString m_Customer_Name;
    bool m_Language_Type;
};

#endif // PRINTERCONTROL_H
